<?php
/**
 * User: hjd-yao
 * Date: 2019/7/19
 * Time: 17:01
 */

namespace Shop\Http\Controllers\Traits;


use App\Models\Shop\ShopCartModel;
use App\Models\Shop\ShopGoodsModel;
use Illuminate\Http\Request;

trait CartTrait
{
    /**
     * @title 添加购物车
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $buy_num = abs($request->input('number',1));
        $specs = $request->input('goods_specs');
        $goods_id = $request->input('goods_id');
        $user_id = $request->input('user_id');
        $goods = ShopGoodsModel::find($goods_id);
        if(!$goods&&$goods->status<1){
            return response()->json(['status'=>0,'msg'=>'商品已下架']);
        }
        $hasNum = ShopCartModel::where('user_id',$user_id)->count();
        if($hasNum>=config('shop.cart_max')){
            return response()->json(['status'=>0,'msg'=>'购物车商品已达上限,请支付后再来购买']);
        }
        $cart = ShopCartModel::where('goods_id',$goods_id)->where('goods_specs',$specs)->first();
        if($cart){
            $cart->num = $cart->num+$buy_num;
            $cart->price = $goods->price;
            $cart->total_price = $cart->num*$goods->price;
        }else{
            $cart = new ShopCartModel();
            $cart->goods_id = $goods_id;
            $cart->user_id = $user_id;
            $cart->goods_specs = $specs;
            $cart->num = $buy_num;
            $cart->price = $goods->price;
            $cart->total_price = $goods->price*$buy_num;
            $cart->client_type = 1;
        }

        if($cart->save()){
            return response()->json(['status'=>1,'msg'=>'添加成功']);
        }
        return response()->json(['status'=>0,'msg'=>'添加失败']);
    }

    public function update(Request $request,$cart_id)
    {
        $cart = ShopCartModel::where('user_id',$request->input('user_id'))->find($cart_id);
        if(!$cart){
            return response()->json(['status'=>0,'msg'=>'购物车无该商品']);
        }

        $number = $request->post('number',1);
        $number = abs(intval($number));
        $number = $number>20?20:$number;
        $cart->num = $number;
        if($cart->save()){
            return response()->json(['status'=>1,'msg'=>'更新成功']);
        }
        return response()->json(['status'=>0,'msg'=>'更新失败']);
    }

    /**
     * @title 删除购物车中的商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $cart_id = array_filter(str2arr($request->input('cart_id')));
        if(!$cart_id){
            return response()->json(['status'=>0,'msg'=>'请选择要删除的商品']);
        }

        if(ShopCartModel::where('user_id',$request->input('user_id'))->whereIn('id',$cart_id)->delete()){
            return response()->json(['status'=>1,'msg'=>'删除成功']);
        }
        return response()->json(['status'=>0,'msg'=>'删除失败']);
    }


    /**
     * @title 清空购物车
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function clearAll(Request $request)
    {
        if(ShopCartModel::where('user_id',$request->input('user_id'))->delete()){
            return response()->json(['status'=>1,'msg'=>'清空购物车成功']);
        }
        return response()->json(['status'=>0,'msg'=>'清空购物车失败']);
    }
}